﻿using System.Data;

namespace SalaryCom.Cool.Gadgets
{
    public class Seog_Jmt_Data
    {
        public void SalCleanDataSet(ref DataSet dstData)
        {
            if (dstData != null)
            {
                dstData.Dispose();
                dstData = null;
            }
        }

        public void SalCleanDataTable(ref DataTable dtbData)
        {
            if (dtbData != null)
            {
                dtbData.Dispose();
                dtbData = null;
            }
        }

        public void SalCleanDataView(ref DataView dvwData)
        {
            if (dvwData != null)
            {
                dvwData.Dispose();
                dvwData = null;
            }
        }

        public string SalRowFilterClear(string strValue)
        {
            if (string.IsNullOrEmpty(strValue))
                return "";
            strValue = strValue.Replace("'", "''");
            strValue = strValue.Replace("<", "&lt;");
            strValue = strValue.Replace(">", "&gt;");
            return strValue;
        }

        public string SalClearSqlInjectCode(string strValue)
        {
            if (string.IsNullOrEmpty(strValue))
            {
                return string.Empty;
            }
            return strValue.Replace("'", "''");
        }

        public bool SalIsEmptyDataSet(DataSet dstData)
        {
            if (dstData != null && dstData.Tables.Count > 0 && dstData.Tables[0].Rows.Count > 0 && !dstData.Tables[0].Columns.Contains("row"))
                return false;
            else
                return true;
        }

        public bool SalIsEmptyDataTable(DataSet dstData, int intDataTableIndex = 0)
        {
            if (dstData == null || dstData.Tables.Count <= intDataTableIndex)
            {
                return true;
            }
            return dstData.Tables[intDataTableIndex].Rows.Count == 0;
        }

        public bool SalIsEmptyDataTable(DataTable dtbDataTable)
        {
            if (dtbDataTable == null || dtbDataTable.Rows.Count == 0)
            {
                return true;
            }
            return false;
        }

        public int SalGetDataSetRowsNum(DataSet dstData)
        {
            if (!SalIsEmptyDataSet(dstData))
            {
                return dstData.Tables[0].Rows.Count;
            }
            else
                return 0;
        }

        public int SalGetDataTableRowsNum(DataTable dtbData)
        {
            if (dtbData == null)
            {
                return 0;
            }
            return dtbData.Rows.Count;
        }

        public DataTable SalGetFirstDataTable(DataSet dstData)
        {
            if (!SalIsEmptyDataSet(dstData))
            {
                return dstData.Tables[0];
            }
            else
            {
                return null;
            }
        }

        public DataRow SalGetFirstDataRow(DataSet dstData)
        {
            if (SalGetDataSetRowsNum(dstData) > 0)
            {
                return dstData.Tables[0].Rows[0];
            }
            return null;
        }

        public string SalGetFirstDataCell(DataSet dstData, string strColumnName)
        {
            if (SalGetDataSetRowsNum(dstData) > 0 && dstData.Tables[0].Columns.Contains(strColumnName))
            {
                return dstData.Tables[0].Rows[0][strColumnName].ToString();
            }
            return string.Empty;
        }


    }
}